from fastapi import APIRouter, Request
from fastapi import Depends
from requests import Session

from config.get_db import get_db
from core.entity.vo.common import CrudResponse
from core.entity.vo.view_models import CmsAlertMessageVO, CmsAlertMessageQuery, CmsAlertMessageQueryPage
from core.service.cms_alert_message_service import CmsAlertMessageService
from utils.log_util import *
from utils.page_util import get_new_page_obj
from utils.response_util import *
from utils.time_format_util import list_format_datetime

alertMessageController = APIRouter()


@alertMessageController.post("/alert_message/query_by_id", response_model=CmsAlertMessageVO)
async def get_alert_message_by_id(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_alert_message = CmsAlertMessageService.get_cms_alert_message_by_id(query_db, id)
        logger.info('获取成功')
        return response_200(data=cms_alert_message, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@alertMessageController.post("/alert_message/query_list", response_model=CmsAlertMessageVO, summary="围栏报警")
async def get_alert_message(request: Request, cms_alert_message_query: CmsAlertMessageQueryPage,
                            query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据、
        cms_alert_message, total = CmsAlertMessageService.get_cms_alert_message(query_db,cms_alert_message_query)
        list_format_datetime(cms_alert_message)
        # 分页操作
        page_result_after = get_new_page_obj(cms_alert_message, cms_alert_message_query.page_num, cms_alert_message_query.page_size, total)
        logger.info('获取成功')
        return response_200(data=page_result_after, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@alertMessageController.post("/alert_message/query_like_fence_name", response_model=CmsAlertMessageVO)
async def get_alert_message_like_fence_name(request: Request, fence_name: str, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_alert_message_list = CmsAlertMessageService.get_cms_alert_message_like_name(query_db, fence_name)
        logger.info('获取成功')
        return response_200(data=cms_alert_message_list, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@alertMessageController.post("/alert_message/add", response_model=CrudResponse)
async def add_alert_message(request: Request, cms_alert_message: CmsAlertMessageVO,
                            query_db: Session = Depends(get_db)):
    try:
        cms_alert_message_result = await CmsAlertMessageService.add_cms_alert_message(query_db,
                                                                                      cms_alert_message)
        if cms_alert_message_result.is_success:
            logger.info(cms_alert_message_result.message)
            return response_200(data=cms_alert_message_result, message=cms_alert_message_result.message)
        else:
            logger.warning(cms_alert_message_result.message)
            return response_400(data="", message=cms_alert_message_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@alertMessageController.patch("/alert_message/edit", response_model=CrudResponse)
async def edit_alert_message(request: Request, cms_alert_message: CmsAlertMessageVO,
                             query_db: Session = Depends(get_db)):
    try:
        cms_alert_message_result = await CmsAlertMessageService.edit_cms_alert_message(query_db, cms_alert_message)
        if cms_alert_message_result.is_success:
            logger.info(cms_alert_message_result.message)
            return response_200(data=cms_alert_message_result, message=cms_alert_message_result.message)
        else:
            logger.warning(cms_alert_message_result.message)
            return response_400(data="", message=cms_alert_message_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@alertMessageController.post("/alert_message/delete", response_model=CrudResponse)
async def delete_alert_message(request: Request, cms_alert_message: CmsAlertMessageVO,
                               query_db: Session = Depends(get_db)):
    try:
        delete_alert_message_result = CmsAlertMessageService.delete_cms_alert_message(query_db, cms_alert_message.id)
        if delete_alert_message_result.is_success:
            logger.info(delete_alert_message_result.message)
            return response_200(data=delete_alert_message_result, message=delete_alert_message_result.message)
        else:
            logger.warning(delete_alert_message_result.message)
            return response_400(data="", message=delete_alert_message_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
